package com.online.servlet.user;

import com.mysql.cj.Session;
import com.online.pojo.*;
import com.online.service.user.Impl.ShopServiceImpl;
import com.online.service.user.Impl.UserServiceImpl;
import com.online.service.user.Impl.billServiceImpl;
import com.online.service.user.Impl.merchantServiceImpl;
import com.online.service.user.ShopService;
import com.online.service.user.UserService;
import com.online.service.user.billService;
import com.online.service.user.merchantService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      String user_number = req.getParameter("user_number");
      String user_name = req.getParameter("user_name");
      String pwd = req.getParameter("pwd");

      UserService userService= new UserServiceImpl();
      User user= userService.login(user_name);
        ShopService shopService = new ShopServiceImpl();
      System.out.println(user_number+user_name+pwd);
      if (user.getUser_name()==null){
          System.out.println("账号为空，去注册");
          req.getRequestDispatcher("/jsp/Submit.jsp").forward(req,resp);
      }
      if (user.getUser_number().equals(user_number)&&user.getUser_pwd().equals(pwd)){
          System.out.println("登录成功");
          req.setAttribute("user_name",user.getUser_name());
          req.setAttribute("account",user.getAccount());
          int i=user.getUser_permission();
          if (i==1) {

                  List<UpComing_Shop> list =new ArrayList<UpComing_Shop>();
              try {
                  shopService.select(list);
                  req.setAttribute("list",list);
                  req.getRequestDispatcher("/jsp/administrator.jsp").forward(req,resp);
              } catch (SQLException e) {
                  e.printStackTrace();
              }


          }
             if (i==2){

                 merchantService merchantService=new merchantServiceImpl();
                 try {
                     merchant shop;
                     shop = merchantService.loginUsername(user_name);
                     req.setAttribute("shop_name",shop.getShop_name());
                     req.setAttribute("user_name",shop.getUser_name());
                     req.setAttribute("account",shop.getAccount());
                     List<menu> list=new ArrayList<menu>();
                     merchantService.select(list,shop.getShop_name());
                     System.out.println(list);
                     req.setAttribute("menu",list);
                     req.getRequestDispatcher("/jsp/Merchantback.jsp").forward(req,resp);
                 } catch (SQLException e) {
                     e.printStackTrace();
                 }

          }
             if (i==3){
                 req.setAttribute("user_name",user.getUser_name());
                 req.setAttribute("account",user.getAccount());
                 List<menu> list=new ArrayList<menu>();
                 try {
                     userService.showAllmenu(list);
                     req.setAttribute("m",list);
                     List<Bill> bill=new  ArrayList<Bill>();
                     billService billService=new billServiceImpl();
                     billService.select(bill,user_name);
                     double value=0;
                     int j=0;
                     while (j<bill.size() ){
                         double price= bill.get(j).getPrice();
                         value+=price;
                         j++;
                     }
                     req.setAttribute("value",value);
                     List<Bill> billed=new  ArrayList<Bill>();
                     billService.selectBilled(billed,user_name);
                     double Billedvalue=0;
                     int q=0;
                     while (q<billed.size() ){
                         double prices= billed.get(q).getPrice();
                         Billedvalue+=prices;
                         q++;
                     }
                     req.setAttribute("Billedvalue",Billedvalue);
                     req.setAttribute("billed",billed);
                     req.setAttribute("bill",bill);
                     req.getRequestDispatcher("/jsp/orderpage.jsp").forward(req,resp);
                 } catch (SQLException e) {
                     e.printStackTrace();
                 }

             }
      }else {
          System.out.println("手机号或密码错误,找回密码！");
          req.setAttribute("a","手机号或密码错误！");
          req.getRequestDispatcher("/jsp/RetrievePassword.jsp").forward(req,resp);
      }
    }
}
